package com.gimi.test_jpa.com.gimi.service.customer;

import cn.hutool.core.bean.BeanUtil;
import com.gimi.test_jpa.com.gimi.dto.customer.CustomerAttachmentDto;
import com.gimi.test_jpa.com.gimi.dto.customer.CustomerDto;
import com.gimi.test_jpa.com.gimi.entity.customer.CustomerAttachmentEntity;
import com.gimi.test_jpa.com.gimi.entity.customer.CustomerEntity;
import com.gimi.test_jpa.com.gimi.jpa.customer.CustomerAttachmentJpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerAttachmentService {
    @Autowired
    private CustomerAttachmentJpa customerAttachmentJpa;

    /**
     * 创建客户附件信息
     *
     * @param dto
     * @return
     */
    public Boolean createCusAtt(CustomerAttachmentDto dto) {
        if (dto.getAttId() != null) {
            CustomerAttachmentEntity customerAttachmentEntity = customerAttachmentJpa.queryByAttId(dto.getAttId());
            if (customerAttachmentEntity != null)
                throw new RuntimeException("该客户附件信息已存在");
        }
        if (dto.getCusId()!=null){
            CustomerAttachmentEntity customerAttachmentEntity = customerAttachmentJpa.queryByCusId(dto.getCusId());
            if (customerAttachmentEntity != null)
                throw new RuntimeException("该客户已经有附件信息");
        }

        CustomerAttachmentEntity customerAttachment= new CustomerAttachmentEntity();
        BeanUtil.copyProperties(dto, customerAttachment);
//        BeanUtils.copyPropertiesIgnoreNull(dto, customerAttachment);
        customerAttachment.setAddTime(new Date());
        customerAttachmentJpa.save(customerAttachment);
        return true;
    }

    /**
     * 根据附件信息Id查询附件信息
     * @param attId
     * @return
     */
    public CustomerAttachmentDto findByAttId(String attId){
        CustomerAttachmentEntity customerAttachment= customerAttachmentJpa.queryByAttId(attId);
        if (customerAttachment==null){
            throw new RuntimeException("该附件信息不存在");
        }
        CustomerAttachmentDto dto = new CustomerAttachmentDto();
        BeanUtil.copyProperties(customerAttachment,dto);
        return dto;
    }

    /**
     * 根据客户Id查询附件信息
     * @param cusId
     * @return
     */
    public CustomerAttachmentDto findByCusId(String cusId){
        CustomerAttachmentEntity customerAttachment= customerAttachmentJpa.queryByCusId(cusId);
        if (customerAttachment==null){
            throw new RuntimeException("该附件信息不存在");
        }
        CustomerAttachmentDto dto = new CustomerAttachmentDto();
        BeanUtil.copyProperties(customerAttachment,dto);
        return dto;
    }

    /**
     * 修改客户附件信息
     * @param dto
     * @return
     */
    public Boolean updateCusAtt(CustomerAttachmentDto dto) {
        CustomerAttachmentEntity customerAttachmentEntity = customerAttachmentJpa.queryByAttId(dto.getAttId());
        if (customerAttachmentEntity == null) {
            throw new RuntimeException("该附件信息不存在");
        }
        if (dto.getCusId()!=customerAttachmentEntity.getCusId()){
            throw new RuntimeException("该附件信息的客户ID不能修改");
        }
        CustomerAttachmentEntity customerAttachment = new CustomerAttachmentEntity();
        BeanUtil.copyProperties(dto, customerAttachment);
//        BeanUtils.copyPropertiesIgnoreNull(dto,customerEntity);
        customerAttachmentJpa.save(customerAttachment);
        return true;
    }

    /**
     * 根据客户附件Id 删除附件信息
     * @param attId
     * @return
     */
    public Boolean deleteCusAtt(String attId){
        CustomerAttachmentEntity customerAttachmentEntity = customerAttachmentJpa.queryByAttId(attId);
        if (customerAttachmentEntity==null){
            throw new RuntimeException("该客户不存在");
        }
        customerAttachmentJpa.deleteById(attId);
        return true;
    }
}
